package cn.com.dcsgo.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 获取Cookie信息
 *
 * @author Dcsgo
 * @since 2025/3/2 14:16
 */
@Controller
@RequestMapping("cookie")
public class CookieController {

    /**
     * 要想获取Cookie信息，需要使用@CookieValue注解
     * 使用CookieValue注解指定接收Cookie信息的参数即可
     * value name 是Cookie的name
     * required 是否必须
     * defaultValue 默认值
     *
     * @param username username
     * @return String
     */
    @ResponseBody
    @GetMapping("data")
    public String getCookie(@CookieValue String username) {
        System.out.println("cookie.username = " + username);
        return "cookie.username = " + username;
    }

    @ResponseBody
    @GetMapping("save")
    public String saveCookie(HttpServletResponse response) {
        Cookie cookie = new Cookie("username", "dcsgo");
        response.addCookie(cookie);
        return "ok";
    }
}
